package com.example.testapp.activity.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.testapp.R;

/**
 * Created by l on 2016/9/5.
 */
public class ErrorHintView extends RelativeLayout {

    public RelativeLayout mContainer;
    public LayoutParams layoutParams;

    /**
     * anim
     * @param context
     */
    public AnimationDrawable animationDrawable;

    public ErrorHandler  mErrorHandler = new ErrorHandler();
    public OperateListener mOperateListener;

    public interface  OperateListener{
        void operate();
    }

    public ErrorHintView(Context context) {
        super(context);
        init();
    }

    public ErrorHintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_hint_custom_error, this);
        mContainer = (RelativeLayout)findViewById(R.id.container);
        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    private void show() {
        setVisibility(View.VISIBLE);
    }


    private void close() {
        setVisibility(View.GONE);
    }

    /**
     * 锦囊 用于实施策略,处理订单状态
     *
     * @author
     */
    public class ErrorHandler{
        public ErrorHandler(){

        }
        public void operate(IStrategy strategy){
            show();
            strategy.operate();
        }
    }

    public View loadingdata;
    /**
     * 转菊花
     *
     * @author 正在加载
     */
    public class LoadingData implements IStrategy{

        @Override
        public void operate() {
           if(loadingdata == null){
               loadingdata = View.inflate(getContext(), R.layout.view_layout_load_loading, null);
           }
            ImageView iv = (ImageView) loadingdata.findViewById(R.id.loading_iv);
            mContainer.removeAllViews();
            mContainer.addView(loadingdata, layoutParams);
            showLoading(iv);
        }
    }



    /**
     * 转菊花
     *
     * @param
     */
    public void loadingdata() {
        mErrorHandler.operate(new LoadingData());
    }

    /**
     * 显示无数据
     */
    public void noData() {
        mErrorHandler.operate(new NoDataError());
    }

    public View noData;
    /**
     * 无数据
     *
     * @author
     */
    public class NoDataError implements IStrategy{

        @Override
        public void operate() {
             if(noData == null){
                 noData = View.inflate(getContext(), R.layout.view_layout_load_noorder, null);
             }
            mContainer.removeAllViews();
            mContainer.addView(noData, layoutParams);
        }
    }

    /**
     * 显示无网络
     */
    public void netError(OperateListener Listener) {
        this.mOperateListener = Listener;
        mErrorHandler.operate(new NetWorkError());
    }

    public View netError;
    /**
     * 无网络
     *
     * @author
     *
     */
    public class NetWorkError implements IStrategy{

        @Override
        public void operate() {
           if(netError == null){
               netError = View.inflate(getContext(), R.layout.view_layout_load_wifi_failure, null);
               View view = netError.findViewById(R.id.wifi_retry);
               view.setOnClickListener(new OnClickListener() {
                   @Override
                   public void onClick(View v) {
                        mOperateListener.operate();
                   }
               });
           }
            mContainer.removeAllViews();
            mContainer.addView(netError, layoutParams);
        }
    }

    public void loadFailure(OperateListener Listener) {
        this.mOperateListener = Listener;
        mErrorHandler.operate(new LoadFailure());
    }

    /**
     * 显示动画loading
     */
    private void showLoading(ImageView iv) {
        animationDrawable = (AnimationDrawable) iv.getBackground();
        animationDrawable.start();
    }

    /**
     * 隐藏动画loading
     */
    public void hideLoading() {
       if(animationDrawable != null && animationDrawable.isRunning()){
           animationDrawable.stop();
       }
        close();
    }


    /**
     * 显示加载失败UI
     */


    public View loadFailure;
    /**
     * 加载失败
     *
     * @author
     */
    public class LoadFailure implements IStrategy{

        @Override
        public void operate() {
            if(loadFailure == null){
                loadFailure = View.inflate(getContext(), R.layout.view_layout_load_failure, null);
                View view = loadFailure.findViewById(R.id.load_retry);
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOperateListener.operate();
                    }
                });
            }
            mContainer.removeAllViews();
            mContainer.addView(loadFailure, layoutParams);
        }
    }
}
